package cn.edu.xidian.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.edu.xidian.R;
import cn.edu.xidian.domain.User;
import cn.edu.xidian.untils.IDUntils;
import cn.edu.xidian.untils.IntentCodeUntils;

import java.util.ArrayList;

public class AddActivity extends Activity {

    private ArrayList<User> users = new ArrayList<User>();//保存新添加的所有数据

    //获取到该页面的组件
    private EditText etWebsite;
    private EditText etUsername;
    private EditText etPwd;
    //按钮对象
    private Button btnConfirm;
    private Button btnClear;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add);

        etWebsite = ((EditText) findViewById(R.id.etWebsite));
        etUsername = ((EditText) findViewById(R.id.etUsername));
        etPwd = ((EditText) findViewById(R.id.etPwd));
        btnConfirm = (Button)findViewById(R.id.btnConfirm);
        btnClear = (Button)findViewById(R.id.btnClear);

        btnConfirm.setOnClickListener(ls);
        btnClear.setOnClickListener(ls);

        //获取intent
        Intent intent = getIntent();
        //构造bundle
        Bundle bundle = new Bundle();
        //给Bundle输入数据

        bundle.putParcelableArrayList("newUsersList", users);
        intent.putExtras(bundle);
        //设置返回码
        setResult(IntentCodeUntils.RESULT_ADD,intent);
    }

    /**
     * 添加和清空按钮监听
     */
    View.OnClickListener ls = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btnConfirm:
                    if(!"".equalsIgnoreCase(etWebsite.getText().toString())){//网站名称不为空
                        User user = new User(
                                IDUntils.getId(),
                                etWebsite.getText().toString(),
                                etUsername.getText().toString(),
                                etPwd.getText().toString()
                        );
                        Toast.makeText(AddActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
                        users.add(user);
                    }else{
                        Toast.makeText(AddActivity.this,"网站信息不能为空",Toast.LENGTH_SHORT).show();
                    }
                    break;

                case R.id.btnClear:
                    etWebsite.setText("");
                    etUsername.setText("");
                    etPwd.setText("");
                    break;
            }
        }
    };
}
